<template>
  <div style="margin-left: 50px;margin-top: 20px;">
    <el-form ref="dataForm" :model="form" label-position="left" label-width="70px" style="width: 400px;">
      <el-form-item label="旧的密码" prop="oldPwd">
        <el-input v-model="form.oldPwd" placeholder="请输入旧的密码" show-password clearable />
      </el-form-item>
      <el-form-item label="新的密码" prop="newPwd">
        <el-input v-model="form.newPwd" placeholder="请输入新的密码" show-password clearable />
      </el-form-item>
      <el-form-item label="确认密码" prop="newPwd2">
        <el-input v-model="form.newPwd2" placeholder="请输入新的密码" show-password clearable />
      </el-form-item>
    </el-form>

    <el-button type="primary" @click="newPwd()">
      确定修改
    </el-button>

  </div>
</template>

<script>
  import {
    Message
  } from "element-ui";
  import {
    newPwd
  } from '@/network/newPwd.js'
  import {
    setToken
  } from '@/utils/auth.js'
  export default {
    name: "newPwd",
    data() {
      return {
        form: {
          oldPwd: '',
          newPwd: '',
          newPwd2: ''
        }
      }
    },
    methods: {
      newPwd() {
        //console.logog(this.form, this.form.newPwd2)
        if (this.form.oldPwd.length == 0 || this.form.newPwd.length == 0) {
          Message.error({
            message: "密码不可为空",
            duration: 1000,
            offset: 200
          });
          return
        } else if (this.form.newPwd != this.form.newPwd2) {
          Message.error({
            message: "两次输入的密码不一致",
            duration: 1000,
            offset: 200
          });
          return
        }


        newPwd(this.form)
          .then((response) => {
            let res = response.data;
            if (res.code == 200) {
              Message.success(res.msg);
              setToken('');
              window.top.location.href = '#/login';
            } else {
              Message.error(res.msg);
            }
          })
          .catch((error) => {
            Message.error({message:"网络异常",offset:200});
            return false;
          });

      },
    }
  }
</script>

<style>
</style>
